import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../lib_baseview.dart';
import '../../utils/constant/app_colors.dart';

// ignore: must_be_immutable
class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
  /// 主标题
  final String? title;

  /// 标题背景
  final Color? backColor;

  /// 返回按钮
  final String? backIconStr;

  /// 按钮回调
  final VoidCallback? onBackTap;
  final List<Widget>? prefixes;
  final List<Widget>? actions;
  final Brightness? brightness;

  /// 背景色（不传透明）
  final Color backgroundColor;
  final BoxBorder? border;
  final Gradient? gradient;
  final WidgetBuilder? titleBuilder;
  double? _height;

  CustomAppBar({
    super.key,
    this.titleBuilder,
    this.title,
    this.backColor,
    this.backIconStr,
    this.onBackTap,
    this.prefixes,
    this.backgroundColor = Colors.transparent,
    this.actions,
    this.border,
    this.brightness,
    this.gradient,
  });

  @override
  Widget build(BuildContext context) {
    _height ??= kToolbarHeight + MediaQuery.of(context).padding.top;
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: systemOverlayStyleForBrightness(
        brightness ?? ThemeData.estimateBrightnessForColor(backgroundColor),
      ),
      child: Container(
        alignment: Alignment.center,
        height: _height ?? kToolbarHeight,
        padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
        decoration: BoxDecoration(
            color: backgroundColor, gradient: gradient, border: border),
        child: Stack(
          alignment: Alignment.center,
          children: [
            const SizedBox.expand(),
            titleBuilder != null
                ? titleBuilder!(context)
                : ConstrainedBox(
                    constraints: const BoxConstraints(maxWidth: 200),
                    child: Text(
                      title ?? '',
                      style: const TextStyle(color: Colors.black),
                    ),
                  ),
            PositionedDirectional(
              start: 0,
              child: GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTap: onBackTap ?? () => Navigator.of(context).pop(),
                child: Container(
                  height: kToolbarHeight,
                  padding: EdgeInsetsDirectional.only(
                      start: 16, end: prefixes != null ? 0 : 16),
                  child: Row(
                    children: [
                      image(
                        backIconStr ?? 'common/common_back_icon.png',
                        width: 32,
                        height: 32,
                      ),
                      if (prefixes != null) ...prefixes!
                    ],
                  ),
                ),
              ),
            ),
            if (actions != null)
              PositionedDirectional(
                end: 0,
                child: Container(
                  height: kToolbarHeight,
                  child: Row(
                    children: actions!,
                  ),
                ),
              )
          ],
        ),
      ),
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(_height ?? kToolbarHeight);
}
